package com.androidmvc.base.ui.fragment;


import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.androidmvc.base.constant.Constant;
import com.androidmvc.base.core.UIEventListener;
import com.androidmvc.base.network.http.okhttp.HttpCycleContext;
import com.androidmvc.base.network.http.okhttp.HttpTaskHandler;


/**
 * Created by jmh on 2016/8/19.
 */
public abstract class BaseFragment extends Fragment implements UIEventListener, HttpCycleContext {

    protected Activity mActivity;
    protected final String HTTP_TASK_KEY = "HttpTaskKey_" + hashCode();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(getViewId(), container, false);
        onEvent(rootView);
        return rootView;
    }

    @Override
    public void onUIEvent(final Object sender, final int event, final Object data) {
        if (getActivity() != null) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    handleUIEvent(sender, event, data);
                }
            });
        }
    }

    /**
     * 子类需要处理事件的话，重写该方法
     */
    public abstract void handleUIEvent(Object sender, int event, Object data);

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mActivity = getActivity();
        initViews();
        onLoadDatas();
    }

    @Override
    public String getHttpTaskKey() {
        return HTTP_TASK_KEY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        HttpTaskHandler.getInstance().removeTask(HTTP_TASK_KEY);
    }

    protected abstract int getViewId();

    protected abstract void initViews();

    protected abstract void onLoadDatas();

    protected abstract void onEvent(View view);


    /**
     * 申请指定的权限.
     */
    public void requestPermission(int code, String... permissions) {

        if (Build.VERSION.SDK_INT >= 23) {
            requestPermissions(permissions, code);
        }
    }

    /**
     * 判断是否有指定的权限
     */
    public boolean hasPermission(String... permissions) {
        for (String permisson : permissions) {
            if (ContextCompat.checkSelfPermission(getActivity(), permisson)
                    != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case Constant.HARDWEAR_CAMERA_CODE:
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    doOpenCamera();
                }
                break;
            case Constant.WRITE_READ_EXTERNAL_CODE:
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    doWriteSDCard();
                }
                break;
        }
    }

    public void doOpenCamera() {

    }

    public void doWriteSDCard() {

    }

}
